package com.wanmait.computershop.dao;

import com.wanmait.computershop.util.DBHelper;
import com.wanmait.computershop.vo.CateGory;
import com.wanmait.computershop.vo.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class WebIndexDao {
    //查询商品分类
    public List<CateGory> findProductsType(){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT * FROM category WHERE parent_id = 161";

        try {
            List<CateGory> productTypeList  = queryRunner.query(sql,new BeanListHandler<>(CateGory.class));
            return productTypeList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    
    //获取前四个新到好货
    public List<Product> findNewProduct(){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT * FROM product order BY add_time DESC LIMIT 4;";

        try {
            List<Product> productList = queryRunner.query(sql,new BeanListHandler<>(Product.class));
            return productList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    //获取前四个热销排行
    public List<Product> findHotProduct(){
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT * FROM product order BY priority DESC LIMIT 4;";

        try {
            List<Product> productList = queryRunner.query(sql,new BeanListHandler<>(Product.class));
            return productList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
